const router = require('koa-router')()
const loginCheck = require('../middleware/loginCheck')
const { createOrder } = require('../controller/order')
const {
    SuccessModel,
    ErrorModel
} = require('../res-model/index')

// 设置前缀
router.prefix('/api/order')

router.post('/', loginCheck,  async (ctx, next) => {
    // 获取订单数据
    const data = ctx.request.body
    // 当前用户名
    const username = ctx.session.userInfo.username
    try{
        const newOrder = await createOrder(username, data)
        ctx.body = new SuccessModel(newOrder)
    }catch(ex) {
        console.error(ex);
        ctx.body = new ErrorModel(10011, "创建订单失败")
    }
})

module.exports = router